
import 'package:cupertino_store/model/product.dart';
import 'package:cupertino_store/styles.dart';
import 'package:flutter/cupertino.dart';
import 'package:provider/provider.dart';

import 'model/app_state_model.dart';

class ProductRowItem extends StatelessWidget {
  
  final Product product;
  final int index;
  final bool lastItem;

  const ProductRowItem(
    {
      super.key, 
      required this.product,
      required this.index,
      required this.lastItem
    }
  );

  @override
  Widget build(BuildContext context) {
    final row = SafeArea(
      top: false,
      bottom: false,
      minimum: const EdgeInsets.only(left: 15,top: 8, bottom: 8,right: 8),
      child: Row(
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.circular(4),
            child: Image.asset(
              product.assetName,
              package: product.assetPackage,
              fit: BoxFit.cover,
              width: 76,
              height: 76,
            ),
          ),
          Expanded(
            child: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 12),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(product.name, style: Styles.productRowItemName),
                  const Padding(padding: EdgeInsets.only(top: 8)),
                  Text('¥:${product.price}', style: Styles.productRowItemPrice)
                ],
              ),
              )
          ),
          CupertinoButton(
            child: const Icon(CupertinoIcons.plus_circle, semanticLabel: 'Add',), 
            onPressed: (){
              final model = Provider.of<AppStateModel>(context, listen: false);
              model.addProductToCart(product.id);
            }
          )
        ],
      )
    );

    if (lastItem) {
      return row;
    }
    return Column(
      children: [        
        row,
        Padding(
          padding: const EdgeInsets.only(left: 100, right: 15),
          child: Container(color: Styles.productRowDivider, height: 1,)
        )
      ],
    );
  }

//   Widget _getRowItem (BuildContext context){
//     log('product - name - ${product.name}');
//     return SafeArea(
//       top: false,
//       bottom: false,
//       minimum: const EdgeInsets.only(left: 15,top: 8, bottom: 8,right: 8),
//       child: Row(
//         children: <Widget>[
//           ClipRRect(
//             borderRadius: BorderRadius.circular(4),
//             child: Image.asset(
//               product.assetName,
//               package: product.assetPackage,
//               fit: BoxFit.cover,
//               width: 76,
//               height: 76,
//             ),
//           ),
//           Expanded(
//             child: Padding(
//               padding: const EdgeInsets.symmetric(horizontal: 12),
//               child: Column(
//                 mainAxisAlignment: MainAxisAlignment.start,
//                 crossAxisAlignment: CrossAxisAlignment.start,
//                 children: [
//                   Text(product.name, style: Styles.productRowItemName),
//                   const Padding(padding: EdgeInsets.only(top: 8)),
//                   Text('¥:${product.price}', style: Styles.productRowItemPrice)
//                 ],
//               ),
//               )
//           ),
//           CupertinoButton(
//             child: const Icon(CupertinoIcons.plus_circle, semanticLabel: 'Add',), 
//             onPressed: (){
//               final model = Provider.of<AppStateModel>(context, listen: false);
//               model.addProductToCart(product.id);
//             }
//           )
//         ],
//       )
//     );
//   }
}